
Partial Class ServerE
    Inherits System.Web.UI.Page

    Private moServer As SQLAutoDocLib.Server = Nothing

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            Dim sSID As String = Request.QueryString("S")

            If IsNumeric(sSID) Then
                '---Edit
                moServer = New SQLAutoDocLib.Server(sSID)
                moServer.Load()

                SID.Value = sSID
                txtName.Text = moServer.Name
                txtDesc.Text = moServer.Desc
                chkIsActive.Checked = moServer.IsActive
            End If

        Else
            If IsNumeric(SID.Value) Then
                '---Edit
                moServer = New SQLAutoDocLib.Server(SID.Value)
                moServer.Load()

                SID.Value = SID.Value
                txtName.Text = moServer.Name
                txtDesc.Text = moServer.Desc
                chkIsActive.Checked = moServer.IsActive
            End If

            If hidToolbarAction.Value = "del" Then
                Dim sSID As String = SID.Value

                '---Delete server
                moServer.Load()
                moServer.IsDeleted = True
                moServer.Save()

                Response.Redirect("SQAbout.aspx")

            ElseIf ValidateFields() Then

                Dim sSID As String = SID.Value

                If IsNumeric(sSID) Then
                    '---Update
                    moServer = New SQLAutoDocLib.Server(sSID)
                    moServer.Load()
                Else
                    '---Add
                    moServer = New SQLAutoDocLib.Server
                End If

                moServer.Name = txtName.Text
                moServer.Desc = txtDesc.Text
                moServer.IsActive = chkIsActive.Checked

                moServer.Save()

                If hidToolbarAction.Value = "newdb" Then
                    Response.Redirect("SQDatabaseE.aspx?S=" & moServer.ID)
                Else
                    hidRefreshParent.Value = "yes"
                End If
            End If
        End If
    End Sub

    Private Function ValidateFields() As Boolean
        Dim bIsValid As Boolean = True

        If Trim(txtName.Text).Length = 0 Then
            errName.InnerText = "You must provide a server name"
            errName.Visible = True

            bIsValid = False
        End If

        Return bIsValid
    End Function

    Protected Function FormLabel() As String
        If Not moServer Is Nothing Then
            Return "Edit " & moServer.Name
        Else
            Return "Add New Server"
        End If
    End Function

    Protected Function ScanMode() As String
        If Not moServer Is Nothing Then
            Return moServer.Name
        Else
            Return "All"
        End If
    End Function

    Protected Function EditMode() As String
        If IsNumeric(SID.Value) Then
            Return "Database"
        Else
            Return "Server"
        End If
    End Function
End Class